using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Threading;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    internal class FormDateRange : CMFormElement
    {
        public override string Value
        {
            get
            {
                TextBox textbox1 = this.FindControl(string.Format("textbox1_{0}", this.ID)) as TextBox;
                TextBox textbox2 = this.FindControl(string.Format("textbox2_{0}", this.ID)) as TextBox;
                return string.Format("{0} - {1}",textbox1.Text, textbox2.Text);
            }
        }

        internal FormDateRange(Forms.Sdk.Items.DateRange element) : base(element)
        {
            PlaceHolder ph = new PlaceHolder();

            //First Date
            System.Web.UI.WebControls.TextBox textbox1 = new TextBox();
            textbox1.ID = string.Format("textbox1_{0}", this.ID);
            
            ph.Controls.Add(textbox1);

            ImageButton button1 = new ImageButton();
            button1.ID = string.Format("button_calendar_1_{0}", this.ID);

            ph.Controls.Add(button1);

            AjaxControlToolkit.CalendarExtender calendar1 = new AjaxControlToolkit.CalendarExtender();
            calendar1.Animated = true;
            calendar1.PopupPosition = AjaxControlToolkit.CalendarPosition.Right;
            calendar1.TargetControlID = textbox1.ID;
            calendar1.PopupButtonID = button1.ID;
            calendar1.Format = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

            if (element.UseNow1)
                calendar1.SelectedDate = DateTime.Now;

            if (element.DefaultValue1.HasValue)
                calendar1.SelectedDate = element.DefaultValue1.Value;

            ph.Controls.Add(calendar1);

            //Second Date
            System.Web.UI.WebControls.TextBox textbox2 = new TextBox();
            textbox2.ID = string.Format("textbox2_{0}", this.ID);

            ph.Controls.Add(textbox2);

            ImageButton button2 = new ImageButton();
            button2.ID = string.Format("button_calendar_2_{0}", this.ID);

            ph.Controls.Add(button2);

            AjaxControlToolkit.CalendarExtender calendar2 = new AjaxControlToolkit.CalendarExtender();
            calendar2.Animated = true;
            calendar2.PopupPosition = AjaxControlToolkit.CalendarPosition.Right;
            calendar2.TargetControlID = textbox2.ID;
            calendar2.PopupButtonID = button2.ID;
            calendar2.Format = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

            if (element.UseNow2)
            {
                calendar2.SelectedDate = DateTime.Now;
            }

            if (element.DefaultValue2.HasValue)
            {
                calendar2.SelectedDate = element.DefaultValue2.Value;
            }

            ph.Controls.Add(calendar2);
            

            Label label = new Label();
            label.Text = element.Label;
            label.CssClass = string.Format("{0}Label", element.CSS);

            #region Validators

            PlaceHolder phValidators = new PlaceHolder();

            //Required
            if (element.Required)
            {
                Label redStar = new Label();
                redStar.ForeColor = Color.Red;
                redStar.Text = "*";

                RequiredFieldValidator required1 = new RequiredFieldValidator();
                required1.ID = string.Format("required1_{0}_{1}", element.FormID, element.ID);
                required1.ControlToValidate = textbox1.ID;
                required1.ValidationGroup = element.FormID.ToString();
                required1.Display = ValidatorDisplay.Dynamic;
                required1.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("Required"), element.Label + " 1"); 

                RequiredFieldValidator required2 = new RequiredFieldValidator();
                required2.ID = string.Format("required2_{0}_{1}", element.FormID, element.ID);
                required2.ControlToValidate = textbox2.ID;
                required2.ValidationGroup = element.FormID.ToString();
                required2.Display = ValidatorDisplay.Dynamic;
                required2.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("Required"), element.Label + " 2"); 
            
                phValidators.Controls.Add(redStar);
                phValidators.Controls.Add(required1);
                phValidators.Controls.Add(required2);
            }

            //Min & Max
            RangeValidator range1 = new RangeValidator();

            range1.Type = ValidationDataType.Date;
            range1.Display = ValidatorDisplay.Dynamic;
            range1.ID = string.Format("range_1_{0}_{1}", element.FormID, element.ID);
            range1.ControlToValidate = textbox1.ID;
            range1.ValidationGroup = element.FormID.ToString();

            if (element.MinValue1.HasValue)
                range1.MinimumValue = element.MinValue1.Value.ToString();
            else
                range1.MinimumValue = DateTime.MinValue.ToShortDateString();

            if (element.MaxValue1.HasValue)
                range1.MaximumValue = element.MaxValue1.ToString();
            else
                range1.MaximumValue = DateTime.MaxValue.ToShortDateString();

            range1.ErrorMessage = string.Format("{0} the date must be between {1} and {2}<br/>", element.Label + " 1", range1.MinimumValue, range1.MaximumValue);

            phValidators.Controls.Add(range1);

            RangeValidator range2 = new RangeValidator();

            range2.Type = ValidationDataType.Date;
            range2.Display = ValidatorDisplay.Dynamic;
            range2.ID = string.Format("range_2_{0}_{1}", element.FormID, element.ID);
            range2.ControlToValidate = textbox2.ID;
            range2.ValidationGroup = element.FormID.ToString();

            if (element.MinValue2.HasValue)
                range2.MinimumValue = element.MinValue2.Value.ToString();
            else
                range2.MinimumValue = DateTime.MinValue.ToShortDateString();

            if (element.MaxValue2.HasValue)
                range2.MaximumValue = element.MaxValue2.ToString();
            else
                range2.MaximumValue = DateTime.MaxValue.ToShortDateString();

            range2.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("DateBetween"), element.Label + " 2", range2.MinimumValue, range2.MaximumValue);

            phValidators.Controls.Add(range2);

            //Compare
            CompareValidator compare = new CompareValidator();

            compare.Type = ValidationDataType.Date;
            compare.ID = string.Format("compare_{0}_{1}", element.FormID, element.ID);
            compare.ControlToValidate = textbox1.ID;
            compare.ControlToCompare = textbox2.ID;
            compare.Display = ValidatorDisplay.Dynamic;
            compare.ErrorMessage = Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("FirstDateLower");
            compare.Operator = ValidationCompareOperator.LessThanEqual;
            compare.ValidationGroup = element.FormID.ToString();

            phValidators.Controls.Add(compare);

            #endregion

            GenerateRow(element, label, ph, phValidators);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            string imageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Alogient.Cameleon.Forms.Sdk.Resources.CalendarIcon.gif");

            ImageButton button1 = this.FindControl(string.Format("button_calendar_1_{0}", this.ID)) as ImageButton;
            button1.ImageUrl = imageUrl;

            ImageButton button2 = this.FindControl(string.Format("button_calendar_2_{0}", this.ID)) as ImageButton;
            button2.ImageUrl = imageUrl;
        }
    }
}
